package com.wuzhazha.redisdemo;

import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.core.RedisTemplate;

/**
  * 订阅类，必须实现MessageListener接口
 * @author yaolong
 * @date 2018/8/6 下午11:25
 */

public class RedisMessageListener implements MessageListener {



    private RedisTemplate redisTemplate02;

    public RedisTemplate getRedisTemplate02() {
        return redisTemplate02;
    }

    public void setRedisTemplate02(RedisTemplate redisTemplate02) {
        this.redisTemplate02 = redisTemplate02;
    }

    @Override
    public void onMessage(Message message, byte[] pattern) {
        //获取消息
        byte[] body = message.getBody();
        //使用值序列化转换器
        String msgBody = (String)getRedisTemplate02().getValueSerializer().deserialize(body);
        System.out.println(msgBody);

        //获取channel
        byte[] channel = message.getChannel();
//        使用字符串序列化转换器
        String channelStr = (String)getRedisTemplate02().getStringSerializer().deserialize(channel);
        System.out.println(channelStr);

        //渠道名称转换
        String patternStr = new String(pattern);
        System.out.println(patternStr);

    }
}
